package com.wut.clock.controller;

import com.wut.clock.common.api.CommonResult;
import com.wut.clock.service.IUserService;
import com.wut.clock.vo.admin.LoginVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.shiro.SecurityUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotBlank;

/**
 * @author ZYH
 * @unit D9Lab
 * @description 登录管理
 * @date 2022/5/30 9:42
 */
@RequiredArgsConstructor
@RequestMapping("/login")
@RestController
@Api(value="登录管理器",tags = {"登录管理器"})
public class PassPortController {

    private final IUserService userService;

    @ApiOperation("注册用户/忘记密码时发送邮件")
    @PostMapping("/sendRegisterMail")
    public CommonResult sendRegisterMail(@RequestParam String type, @RequestParam String mail){
        if(userService.sendRegisterMail(type,mail)) return CommonResult.success("注册邮件发送成功");
        return CommonResult.failed();
    }

    @ApiOperation("注册用户")
    @PostMapping("/register")
    public CommonResult register(@RequestParam String username,
                                 @RequestParam String password,
                                 @RequestParam String confirmPassword,
                                 @RequestParam String email,
                                 @RequestParam String code){
        if(userService.registerUser(username,password,confirmPassword,email,code)>0)
            return CommonResult.success("注册成功！");
        return CommonResult.failed();
    }

    @ApiOperation("忘记密码")
    @PostMapping("/forgetPassword")
    public CommonResult forgetPassword(String email,String code,String password,String confirmPassword){
        if(userService.forgetPassword(email,code,password,confirmPassword)>0) return CommonResult.success("修改密码成功！");
        return CommonResult.failed("修改密码失败！");
    }

    @ApiOperation("登录")
    @PostMapping("/login")
    public CommonResult login(@NotBlank @RequestParam String username,
                              @NotBlank @RequestParam String password){
        LoginVo loginVo = userService.login(username, password);
        return CommonResult.success(loginVo,"登录成功");
    }

    @ApiOperation("退出登录")
    @PostMapping("/logout")
    public CommonResult logout(){
        SecurityUtils.getSubject().logout();
        return CommonResult.success("退出成功");
    }
    
}
